#include "winctl.h"
#include "taskbar.h"
#include <stdio.h>

static bool winctl_left_btn_handler(gui_spirit_t *spirit, void *arg)
{
    winctl_t *winctl = (winctl_t *)arg;
    if (!winctl)
        return false;

    uview_msg_t msg;
    if (winctl == taskbar.last_winctl)
    {
        if (winctl->ishidden) // is hidden window,show it
        {
            uview_msg_header(&msg, UVIEW_MSG_SHOW, winctl->winid);
            uview_send_msg(taskbar.spirit->view, &msg);
        }
        else
        { // is focus window,hidden
            uview_msg_header(&msg, UVIEW_MSG_HIDE, winctl->winid);
            uview_send_msg(taskbar.spirit->view, &msg);
        }
    }
    else
    { // no last winctl,show it
        uview_msg_header(&msg, UVIEW_MSG_SHOW, winctl->winid);
        uview_send_msg(taskbar.spirit->view, &msg);
    }
    return true;
}

winctl_t *winctl_create(int winid)
{
    if (winid < 0)
        return NULL;

    winctl_t *winctl = malloc(sizeof(winctl_t));
    if (!winctl)
        return NULL;
    winctl->button = gui_button_create();
    if (!winctl->button)
    {
        free(winctl);
        return NULL;
    }
    winctl->winid = winid;
    winctl->ishidden = false; // show default
    winctl->isfocus = false;  // no focus

    winctl->button->style.border_color = GUI_COLOR_NONE;
    winctl->button->style.background_color = taskbar.winctl_backcolor ;

    gui_button_t *btn = GUI_BUTTON(winctl->button);
    btn->color_idle = taskbar.winctl_backcolor;
    btn->color_touch = GUI_RGB_ADD(taskbar.winctl_backcolor, 0x40, 0x40, 0x40);
    btn->color_click = GUI_RGB_ADD(taskbar.winctl_backcolor, 0x20, 0x20, 0x20);
    gui_spirit_set_size(winctl->button, WINCTL_SIZE_DEFAULT, WINCTL_SIZE_DEFAULT);
    // click event
    gui_signal_connect(winctl->button, "button_press", winctl_left_btn_handler, winctl);

    list_add_tail(&winctl->list, &taskbar.winctl_list_head);
    gui_container_add(GUI_CONTAINER(taskbar.spirit), winctl->button);
    dprintf("[winctl] create winctl ok\n");
    return winctl;
}

int winctl_destroy(winctl_t *winctl)
{
    if (!winctl)
        return -1;
    gui_container_remove(GUI_CONTAINER(taskbar.spirit), winctl->button);
    list_del_init(&winctl->list);
    if (gui_spirit_destroy(winctl->button) < 0)
        return -1;
    free(winctl);
    return 0;
}

int winctl_destroy_all()
{
    winctl_t *winctl, *winctl_next;
    list_traversal_all_owner_to_next_safe(winctl, winctl_next, &taskbar.winctl_list_head, list)
        winctl_destroy(winctl);
    return 0;
}

winctl_t *winctl_find(int winid)
{
    winctl_t *winctl;
    list_traversal_all_owner_to_next(winctl, &taskbar.winctl_list_head, list)
    {
        if (winctl->winid == winid)
            return winctl;
    }
    return NULL;
}

void winctl_get_focus(winctl_t *winctl)
{
    taskbar.last_winctl = winctl;
    winctl->isfocus = true;
    gui_button_t *btn = GUI_BUTTON(winctl->button);
    btn->color_idle = taskbar.winctl_activecolor;
    btn->spirit.style.background_color = btn->color_idle;
}

void winctl_lose_focus(winctl_t *winctl)
{
    winctl->isfocus = false;
    gui_button_t *btn = GUI_BUTTON(winctl->button);
    btn->color_idle = taskbar.winctl_backcolor;
    btn->spirit.style.background_color = btn->color_idle;
}

void winctl_set_icon(winctl_t *winctl, char *path)
{
    if (!winctl || !path)
        return -1;
    gui_spirit_t *spirit = winctl->button;
    if (!spirit)
        return -1;
    if (gui_spirit_set_image2(spirit, path, WINCTL_ICON_SIZE, WINCTL_ICON_SIZE) < 0)
    {
        // no found icon file,set to default icon
        if (gui_spirit_set_image2(spirit, WINCTL_ICON_PATH_DEFAULT, WINCTL_ICON_SIZE, WINCTL_ICON_SIZE) < 0)
            return -1;
    }
    gui_spirit_show(spirit);
    return 0;
}

void winctl_paint(winctl_t *winctl)
{
    if (winctl)
    {
        gui_spirit_show(winctl->button);
    }
    else
    {
        // show all winctl
        taskbar_drawbackground(); // draw background
        winctl_t *tmp=NULL;
        int x = 80, y = taskbar.spirit->height/2-WINCTL_SIZE_DEFAULT/2;
        list_traversal_all_owner_to_next(tmp, &taskbar.winctl_list_head, list)
        {
            gui_spirit_set_pos(tmp->button, x, y);
            gui_spirit_show(tmp->button);
            x += WINCTL_SIZE_DEFAULT + 2;
            // above screen witdh
            if (x >= taskbar.screen_width - WINCTL_SIZE_DEFAULT)
                break;
        }
    }
}